package com.mcxx.modules.temporary.family.service.write;

import com.mcxx.common.exception.BizException;
import com.mcxx.convert.FamilyConvert;
import com.mcxx.modules.miniSalvation.family.dao.read.FamilyMainReadDao;
import com.mcxx.modules.miniSalvation.family.dao.read.FamilyMemberReadDao;
import com.mcxx.modules.miniSalvation.family.entity.FamilyMainEntity;
import com.mcxx.modules.miniSalvation.family.entity.FamilyMainHistoryEntity;
import com.mcxx.modules.miniSalvation.family.entity.FamilyMemberEntity;
import com.mcxx.modules.miniSalvation.family.entity.FamilyMemberHistoryEntity;
import com.mcxx.modules.miniSalvation.order.dto.OrderDTO;
import com.mcxx.modules.miniSalvation.order.entity.OrderEntity;
import com.mcxx.modules.temporary.family.dao.read.TmpFamilyMainHistoryReadDao;
import com.mcxx.modules.temporary.family.dao.read.TmpFamilyMainReadDao;
import com.mcxx.modules.temporary.family.dao.read.TmpFamilyMemberHistoryReadDao;
import com.mcxx.modules.temporary.family.dao.read.TmpFamilyMemberReadDao;
import com.mcxx.modules.temporary.family.dao.write.TmpFamilyMemberHistoryWriteDao;
import com.mcxx.modules.temporary.family.entity.TmpFamilyMainEntity;
import com.mcxx.modules.temporary.family.entity.TmpFamilyMainHistoryEntity;
import com.mcxx.modules.temporary.family.dao.write.TmpFamilyMainHistoryWriteDao;
import com.mcxx.modules.temporary.family.entity.TmpFamilyMemberEntity;
import com.mcxx.modules.temporary.family.entity.TmpFamilyMemberHistoryEntity;
import com.mcxx.modules.temporary.order.dto.TmpOrderDTO;
import com.mcxx.modules.temporary.order.entity.TmpOrderEntity;
import com.mcxx.util.Constant;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@TaTransactional
public class TmpFamilyMainHistoryWriteService {

    @Autowired
    private TmpFamilyMainHistoryWriteDao tmpFamilyMainHistoryWriteDao;
    @Autowired
    private TmpFamilyMemberHistoryWriteDao tmpFamilyMemberHistoryWriteDao;
    @Autowired
    private TmpFamilyMainReadDao tmpFamilyMainReadDao;
    @Autowired
    private TmpFamilyMemberReadDao tmpFamilyMemberReadDao;






    public int insert(TmpFamilyMainHistoryEntity tmpFamilyMainHistory) {
    	return tmpFamilyMainHistoryWriteDao.insert(tmpFamilyMainHistory);
    }


    public int insertIgnoreNull(TmpFamilyMainHistoryEntity tmpFamilyMainHistory) {
    	return tmpFamilyMainHistoryWriteDao.insertIgnoreNull(tmpFamilyMainHistory);
    }


    public int update(TmpFamilyMainHistoryEntity tmpFamilyMainHistory) {
    	return tmpFamilyMainHistoryWriteDao.update(tmpFamilyMainHistory);
    }


    public int updateMemberName(String name,String orderId,String familyId) {
        return tmpFamilyMainHistoryWriteDao.updateMemberName(name,orderId,familyId);
    }


    public int updateIgnoreNull(TmpFamilyMainHistoryEntity tmpFamilyMainHistory) {
    	return tmpFamilyMainHistoryWriteDao.updateIgnoreNull(tmpFamilyMainHistory);
    }


    public int delete(TmpFamilyMainHistoryEntity tmpFamilyMainHistory) {
    	return tmpFamilyMainHistoryWriteDao.delete(tmpFamilyMainHistory);
    }


    public int backHistory(TmpOrderDTO order, String familyId, UserAccountVo userAccountVo){

        TmpOrderEntity orderEntity = new TmpOrderEntity();
        BeanUtils.copyProperties(order, orderEntity);
        TmpFamilyMainEntity familyMainEntity = tmpFamilyMainReadDao.getById(familyId);
        List<TmpFamilyMemberEntity> familyMemberEntityList = tmpFamilyMemberReadDao.listByFamilyId(familyId);


        String masterState = "01";

        for(TmpFamilyMemberEntity familyMemberEntity : familyMemberEntityList){

            TmpFamilyMemberHistoryEntity tmpFamilyMemberHistoryEntity = FamilyConvert.convertTmpFamilyMemberHistoryEntity(orderEntity, familyMemberEntity, familyMainEntity, userAccountVo);

            tmpFamilyMemberHistoryWriteDao.insertIgnoreNull(tmpFamilyMemberHistoryEntity);
        }


        TmpFamilyMainHistoryEntity tmpFamilyMainHistoryEntity = FamilyConvert.convertTmpFamilyMainHistoryEntity(orderEntity, familyMainEntity, userAccountVo);

        int rst = tmpFamilyMainHistoryWriteDao.insertIgnoreNull(tmpFamilyMainHistoryEntity);
        if(rst <= 0){
            throw new BizException("-1", "操作失败");
        }
        return 1;
    }

    public int deleteByOrderId(String orderId){
        tmpFamilyMemberHistoryWriteDao.deleteByOrderId(orderId);
        return tmpFamilyMainHistoryWriteDao.deleteByOrderId(orderId);
    }
}
